{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "88d47190",
   "metadata": {},
   "source": [
    "# <center>三位一体汉语词法分析字标记</center>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "6d746498",
   "metadata": {},
   "outputs": [],
   "source": [
    "#!/usr/bin/env python\n",
    "# -*- coding: utf-8 -*-\n",
    "# -*- auteur chen_zhao_shi -*-\n",
    "import jieba\n",
    "import time\n",
    "from tkinter import *\n",
    "from tkinter import filedialog\n",
    "import os\n",
    "\n",
    "LOG_LINE_NUM = 0\n",
    "\n",
    "\n",
    "def get_current_time():\n",
    "    current_time = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time()))\n",
    "    return current_time\n",
    "\n",
    "\n",
    "# 界面化\n",
    "class MY_GUI:\n",
    "    def __init__(self, init_window_name):\n",
    "        self.str_trans_to_md5_button = None\n",
    "        self.log_data_Text = None\n",
    "        self.result_data_Text = None\n",
    "        self.log_label = None\n",
    "        self.result_data_label = None\n",
    "        self.init_data_label = None\n",
    "        self.init_data_Text = None\n",
    "        self.init_window_name = init_window_name\n",
    "\n",
    "    def callback(self):\n",
    "        default_dir = r\"文件路径\"\n",
    "        fileName = filedialog.askopenfilename(defaultextension=\".*\",\n",
    "                                              initialdir=(os.path.expanduser(default_dir)))\n",
    "        my_txt_file = fileName\n",
    "        if os.path.exists(my_txt_file):\n",
    "            a = open(my_txt_file, 'r', encoding='utf-8')\n",
    "            self.init_data_Text.delete(1.0, END)\n",
    "            for id_names in a:\n",
    "                self.init_data_Text.insert('insert', id_names)\n",
    "            a.close()\n",
    "\n",
    "    # 设置窗口\n",
    "    def set_init_window(self):\n",
    "        self.init_window_name.title(\"欢迎使用三位一体汉语词法分析字标记系统\")  # 窗口名\n",
    "        # self.init_window_name.geometry('320x160+10+10')                         #290 160为窗口大小，+10 +10 定义窗口弹出时的默认展示位置\n",
    "        self.init_window_name.geometry('1068x681+250+70')\n",
    "        # self.init_window_name[\"bg\"] = \"pink\"\n",
    "        # #窗口背景色，其他背景色见\n",
    "        # 虚化，值越小虚化程度越高 标签\n",
    "        self.init_data_label = Label(self.init_window_name, text=\"待处理数据\")\n",
    "        self.init_data_label.grid(row=1, column=0)\n",
    "        self.result_data_label = Label(self.init_window_name, text=\"输出结果\")\n",
    "        self.result_data_label.grid(row=1, column=12)\n",
    "        self.log_label = Label(self.init_window_name, text=\"日志\")\n",
    "        self.log_label.grid(row=12, column=0)\n",
    "        # 文本框\n",
    "        self.init_data_Text = Text(self.init_window_name, width=67, height=35)  # 原始数据录入框\n",
    "        self.init_data_Text.grid(row=2, column=0, rowspan=10, columnspan=10)\n",
    "        self.result_data_Text = Text(self.init_window_name, width=68, height=47)  # 处理结果展示\n",
    "        self.result_data_Text.grid(row=2, column=12, rowspan=15, columnspan=10)\n",
    "        self.log_data_Text = Text(self.init_window_name, width=66, height=9)  # 日志框\n",
    "        self.log_data_Text.grid(row=13, column=0, columnspan=10)\n",
    "        # 按钮\n",
    "        self.str_trans_to_md5_button = Button(self.init_window_name, text=\"分词标记\", bg=\"lightblue\", width=10,\n",
    "                                              command=self.out_put_data)  # 调用内部方法  加()为直接调用\n",
    "        self.str_trans_to_md5_button.grid(row=2, column=11)\n",
    "        path = StringVar()\n",
    "        path.set(os.path.abspath(\".\"))\n",
    "\n",
    "        # 文档路径选择\n",
    "        Label(self.init_window_name, text=\"目标路径:\").grid(row=0, column=0)\n",
    "        Entry(self.init_window_name, textvariable=path, state=\"readonly\").grid(row=0, column=1, ipadx=90)\n",
    "        Button(self.init_window_name, text=\"选择文件\", command=self.callback).grid(row=0, column=2)  # 调用选择文件路径函数\n",
    "\n",
    "    # 功能函数\n",
    "    def out_put_data(self):\n",
    "\n",
    "        def seg_sentence(sentence):\n",
    "            sentence_seged = jieba.cut(sentence.strip())\n",
    "            stopwords = {\" \", \"，\", \"。\", \"“\", \"”\", '“', \"？\", \"！\", \"：\", \"《\",\n",
    "                         \"》\", \"、\", \"；\", \"·\", \"‘ \", \"’\", \"──\", \",\", \".\", \"?\",\n",
    "                         \"!\", \"`\", \"~\", \"@\", \"#\", \"$\", \"%\", \"^\", \"&\", \"*\", \"(\", \")\",\n",
    "                         \"-\", \"_\", \"+\", \"=\", \"[\", \"]\", \"{\", \"}\",\n",
    "                         '\"', \"'\", \"<\", \">\", \"\\\\\", \"|\" \"\\r\", \"\\n\", \"\\t\"}  # 这里加载停用词\n",
    "            out_str = \"\"\n",
    "            for word in sentence_seged:\n",
    "                if word not in stopwords:\n",
    "                    if word != '\\t':\n",
    "                        out_str += word\n",
    "                        out_str += \" \"\n",
    "            return out_str\n",
    "\n",
    "        src = self.init_data_Text.get(1.0, END)  # 读取文本框内容\n",
    "        # print(\"src =\", src)\n",
    "        if src:\n",
    "            try:\n",
    "                # 将文本框内容转化为字符\n",
    "                src = (src,)\n",
    "                # 将处理结果输出到输出文本框\n",
    "                self.result_data_Text.delete(1.0, END)\n",
    "                for str in src:\n",
    "                    line_seg = seg_sentence(str)\n",
    "                    self.result_data_Text.insert('insert', line_seg)\n",
    "                self.write_log_to_Text(\"分词标注成功！\")\n",
    "            except:\n",
    "                self.result_data_Text.delete(1.0, END)\n",
    "                self.result_data_Text.insert(1.0, \"标记失败！\")\n",
    "        else:\n",
    "            self.write_log_to_Text(\"ERROR:标记失败！\")\n",
    "\n",
    "    # 日志动态打印\n",
    "    def write_log_to_Text(self, logmsg):\n",
    "        global LOG_LINE_NUM\n",
    "        current_time = get_current_time()\n",
    "        logmsg_in = str(current_time) + \" \" + str(logmsg) + \"\\n\"  # 换行\n",
    "        if LOG_LINE_NUM <= 7:\n",
    "            self.log_data_Text.insert(END, logmsg_in)\n",
    "            LOG_LINE_NUM = LOG_LINE_NUM + 1\n",
    "        else:\n",
    "            self.log_data_Text.delete(1.0, 2.0)\n",
    "            self.log_data_Text.insert(END, logmsg_in)\n",
    "\n",
    "\n",
    "def gui_start():\n",
    "    init_window = Tk()  # 实例化出一个父窗口\n",
    "    ZMJ_PORTAL = MY_GUI(init_window)\n",
    "    # 设置根窗口默认属性\n",
    "    ZMJ_PORTAL.set_init_window()\n",
    "\n",
    "    init_window.mainloop()  # 父窗口进入事件循环，可以理解为保持窗口运行，否则界面不展示\n",
    "\n",
    "\n",
    "gui_start()\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
